/*
GANESHA - Reasoning on critical infrastructures affected by emergency situations
Copyright (c) 2010-2011, Information Eng. Research Unit - Univ. of Alcala
http://www.cc.uah.es/ie
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option)
any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License along
with this library; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

package es.uah.cc.ie.ganesha.util;

import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.ObjectProperty;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import org.mindswap.pellet.jena.PelletReasonerFactory;

/**
 *
 * @author ieru
 */
public class RulesExample {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
		String ont = "http://owldl.com/ontologies/dl-safe.owl";

		OntModel model = ModelFactory.createOntologyModel( PelletReasonerFactory.THE_SPEC, null );
		model.read( ont );

		ObjectProperty sibling = model.getObjectProperty( ont + "#sibling" );

		OntClass BadChild = model.getOntClass( ont + "#BadChild" );
		OntClass Child = model.getOntClass( ont + "#Child" );

		Individual Abel = model.getIndividual( ont + "#Abel" );
		Individual Cain = model.getIndividual( ont + "#Cain" );
		Individual Remus = model.getIndividual( ont + "#Remus" );
		Individual Romulus = model.getIndividual( ont + "#Romulus" );

		model.prepare();

		// Cain has sibling Abel due to SiblingRule
		printPropertyValues( Cain, sibling );
		// Abel has sibling Cain due to SiblingRule and rule works symmetric
		printPropertyValues( Abel, sibling );
		// Remus is not inferred to have a sibling because his father is not
		// known
		printPropertyValues( Remus, sibling );
		// No siblings for Romulus for same reasons
		printPropertyValues( Romulus, sibling );

		// Cain is a BadChild due to BadChildRule
		printInstances( BadChild );
		// Cain is a Child due to BadChildRule and ChildRule2
		// Oedipus is a Child due to ChildRule1 and ChildRule2 combined with the
		// unionOf type
		printInstances( Child );
	}

	public static void printPropertyValues(Individual ind, Property prop) {
		System.out.print( ind.getLocalName() + " has " + prop.getLocalName() + "(s): " );
		printIterator( ind.listPropertyValues( prop ) );
	}

	public static void printInstances(OntClass cls) {
		System.out.print( cls.getLocalName() + " instances: " );
		printIterator( cls.listInstances() );
	}

	public static void printIterator(ExtendedIterator i) {
		if( !i.hasNext() ) {
			System.out.print( "none" );
		}
		else {
			while( i.hasNext() ) {
				Resource val = (Resource) i.next();
				System.out.print( val.getLocalName() );
				if( i.hasNext() )
					System.out.print( ", " );
			}
		}
		System.out.println();
	}

}
